import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hotwork/Myapplication.dart';
import 'package:flutter_hotwork/bean/xunjian/tasklistbean.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:flutter_hotwork/xunjiannew/XunJianMapnew.dart';

import '../EventBusUtils.dart';
import '../Toast.dart';

class XunJianListScreen extends StatefulWidget {
  @override
  XunJianListState createState() => new XunJianListState();
}

tasklistbean tasklist;
bool hasdata = false;

class XunJianListState extends State<XunJianListScreen> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // gettaskList();
    bus.on("flush", (arg) {
      gettaskList();
    });
  }

  var bus = EventBus();

  void gettaskList() async {
    // showDialog(
    //     context: context,
    //     builder: (context) {
    //       return new LoadingDialog();
    //     });
    // Navigator.pop(context);
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX +
          "/checkTask/listAppCheckTask?checkUser=" +
          MyApplication.loginuserinfo.userInfo.username);
      Map respMap = json.decode(response.data);
      tasklist = tasklistbean.fromJson(respMap);
      // Navigator.pop(context);
      if (tasklist.code == 0) {
        hasdata = true;
        setState(() {});
      } else {
        LocalToast.toast(context, msg: tasklist.message);
      }
    } catch (e) {
      // Navigator.pop(context);
    }
  }

  // void uptask(var id,var status) async {
  //   var reqeuest =Map<Object,Object>();
  //   reqeuest["id"] = id;
  //   reqeuest["taskStatus"] = status;
  //   reqeuest["checkStartTime"] = new DateTime.now().toString();
  //   try {
  //     Response response;
  //     response = await DioUtils.dio.put(DioUtils.API_PREFIX+"/checkTask/updateCheckTaskByIdApp", data: json.encode(reqeuest));
  //
  //     Map respMap = json.decode(response.data);
  //     basebean base = basebean.fromJson(respMap);
  //     print('响应数据：' + response.toString());
  //   } catch (e) {
  //     print('请求出错：' + e.toString());
  //   }
  // }
  @override
  Widget build(BuildContext context) {
    ScrollController controller = ScrollController();
    double rpx = MediaQuery.of(context).size.width / 750;
    if (!hasdata) {
      gettaskList();
    }
    return Scaffold(
      appBar: new BaseViewBar(
          childView: new BaseTitleBar(
            "巡检",
            leftIcon: '',
            rightIcon: 'images/refreshwhite.png',
            rightClick: () {
              gettaskList();
            },
            leftClick: () {},
          ),
          preferredSize: Size.fromHeight(50.0)),
      body: hasdata
          ? Container(
              child: Column(
                children: [
                  Expanded(
                      child: RefreshIndicator(
                    onRefresh: () async {
                      setState(() {
                        gettaskList();
                      });
                    },
                    child: genContentList(tasklist.result, context, controller),
                  )),
                ],
              ),
            )
          : Container(),
    );
  }

  String getstatus(int status) {
    if (status == 0) {
      return "未完成";
    } else if (status == 1) {
      return "进行中";
    } else if (status == 2) {
      return "已完成";
    }
  }

  MaterialColor getstatusColor(int status) {
    if (status == 0) {
      return Colors.red;
    } else if (status == 1) {
      return Colors.blue;
    } else if (status == 2) {
      return Colors.green;
    }
  }

  String gettimetisk(int period) {
    if (period == 0) {
      return "每周一次";
    } else if (period == 1) {
      return "每月一次";
    } else if (period == 3) {
      return "每季度一次";
    } else if (period == 4) {
      return "每年一次";
    } else if (period == 5) {
      return "临时任务";
    }
  }

  genContentList(List<Taskbean> data, BuildContext context, controller) {
    double rpx = MediaQuery.of(context).size.width / 750;
    return ListView.builder(
        shrinkWrap: true,
        controller: controller,
        physics: new AlwaysScrollableScrollPhysics(),
        itemCount: data.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(5)),
              boxShadow: <BoxShadow>[
                //设置阴影
                new BoxShadow(
                  color: Colors.grey, //阴影颜色
                  blurRadius: 10.0, //阴影大小
                ),
              ],
            ),
            margin: EdgeInsets.only(left: 10, top: 10, right: 10, bottom: 10),
            height: 310 * rpx,
            child: Column(
              children: <Widget>[
                Container(
                  height: 10,
                ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: 10,
                      ),
                      Text(
                        data[index].checkName,
                        style: new TextStyle(
                          color: Color(0xFF333333),
                          fontSize: 12.0,
                        ),
                      ),
                      Flexible(fit: FlexFit.tight, child: SizedBox()),
                      Container(
                        height: 20,
                        padding: EdgeInsets.only(
                            left: 5, top: 0, right: 5, bottom: 0),
                        child: Text(
                          getstatus(data[index].taskStatus),
                          style: new TextStyle(
                            color: getstatusColor(data[index].taskStatus),
                            fontSize: 12.0,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  height: 1,
                  margin:
                      EdgeInsets.only(left: 10, top: 5, right: 10, bottom: 0),
                  color: Colors.grey[300],
                ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                  margin:
                      EdgeInsets.only(left: 0, top: 8, right: 0, bottom: 8),
                  child: Row(
                    children: [
                      // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
                      // Container(
                      //   width: 10,
                      // ),
                      // Text("",style: new TextStyle(color: Color(0xFF333333),
                      //   fontSize: 12.0,
                      // ),),
                      // Container(
                      //   width: 30,
                      // ),
                      // Image(height:20,width: 20,image: AssetImage('images/weizhi.png')),
                      Container(
                        width: 10,
                      ),
                      Text(
                        "巡检频率：" + data[index].periodStr,
                        style: new TextStyle(
                          color: Color(0xFF333333),
                          fontSize: 12.0,
                        ),
                      ),
                      Container(
                        width: 50,
                      ),
                      Text(
                        "任务类型：" + data[index].checkTypeStr,
                        style: new TextStyle(
                          color: Color(0xFF333333),
                          fontSize: 12.0,
                        ),
                      ),
                    ],
                  ),
                ),
              Container(
                  padding:
                  EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                  margin:
                  EdgeInsets.only(left: 10, top: 8, right: 0, bottom: 0),
                  child:Row(children: [ Text("巡检人员：" + getUser(data[index].checkInspectorPos),
                      style: new TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 12.0,
                      ),
                      overflow: TextOverflow.ellipsis,
                      maxLines: 15)],),
                ),
                // Container(
                //   padding:
                //       EdgeInsets.only(left: 10, top: 0, right: 10, bottom: 0),
                //   margin: EdgeInsets.only(left: 0, top: 0, right: 0, bottom: 0),
                //   child: Row(
                //     children: [
                //       // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
                //       Container(
                //         width: 10,
                //       ),
                //       Text(
                //         "创建时间：" + data[index].createTime,
                //         style: new TextStyle(
                //           color: Color(0xFF333333),
                //           fontSize: 12.0,
                //         ),
                //       ),
                //     ],
                //   ),
                // ),
                Container(
                  padding:
                      EdgeInsets.only(left: 10, right: 0, top: 0, bottom: 0),
                  child: Row(children: [
                    Container(
                      width: 10,
                    ),
                    Text(
                      "创建时间：" + data[index].createTime,
                      style: new TextStyle(
                        color: Color(0xFF333333),
                        fontSize: 12.0,
                      ),
                    ),
                    // Image(height:20,width: 20,image: AssetImage('images/mingcheng.png')),
                  Container(
                    margin:
                    EdgeInsets.only(left: 40, top: 0, right: 0, bottom: 0),
                      padding:
                      EdgeInsets.only(left: 0, right: 10, top: 0, bottom: 0),
                    child:
                    ButtonTheme(
                      minWidth: 100.0, //设置最小宽度
                      height: 20.0,
                      child: RaisedButton(
                        onPressed: () {
                          Navigator.push(context,
                              MaterialPageRoute(builder: (context) {
                            MyApplication.nowtaskbean = data[index];
                            // if(data[index].taskStatus==0){
                            //   // uptask(data[index].id,1);
                            // }
                            return new XunjianNewPage();
                          }));
                        },
                        child: Text(
                          '执行任务',
                          style: new TextStyle(color: Colors.white),
                        ),
                        color: Colors.blue,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(15)),
                        ),
                      ),
                    ),)
                  ]),
                ),
              ],
            ),
          );
        });
  }

  String getUser(List<CheckInspectorPos> users) {
    var user = "";
    if (users.length > 0) {
      for (int i = 0; i < users.length; i++) {
        user = user + users[i].checkUserName + "  ";
      }
    }
    return user;
  }
}
